<?php

namespace Common\Model;

use Think\Model;

class LockorderModel extends Model
{

    protected function _initialize()
    {
        $this->trueTableName = 'lockorder';
    }

    /**
     * 保存订单
     */
    public function saveOrder($orderInfo)
    {
        return $this->add($orderInfo);
    }

    /**
     * 找到用户最近一条订单
     */
    public function findLastOne($userID)
    {
        $value = $this->where("wxuserid={$userID}")->order("id DESC")->find();
        return $value;
    }

    /**
     *  根据订单ID获取订单信息
     */
    public function getOrderInfo($orderid)
    {
        $condition = array("id"=>$orderid);
        return $this->where($condition)->limit(1)->find();
    }

    /**
     *  根据订单ID获取订单信息
     */
    public function getOrderInfoByPhone($phone)
    {
        $condition = array("id"=>$phone);
        return $this->where($condition)->limit(1)->find();
    }


    /**
     * 根据订单ID获取订单信息/支付信息
     */
    public function getOrderPayInfo($orderid)
    {
        $sql = "SELECT * FROM wx_orders a
                join wx_pays b on a.id=b.order_id
                WHERE a.id = '{$orderid}'";
        $result = $this->query($sql);
        return $result;
    }

    /**
     * 通过userid查找
     *
     * @param $userID
     * @return array
     */
    function findByUserID($userID, $service = false)
    {
        $sql = "SELECT * FROM wx_orders WHERE wxuserid='{$userID}'";
        if (!empty($service)) {
            $sql .= " AND service='{$service}'";
        }

        $sql .= " ORDER BY addtime DESC limit 20";
        $result = $this->query($sql);
        return $result;
    }

    /**
     * 通过guestphone查找
     *
     * @param $guestPhone
     * @return array
     */
    function findByguestPhone($guestPhone, $userID, $service = false)
    {
        if (empty($guestPhone)) {
            return false;
        }

        $condition = array(
            "guestphone" => $guestPhone,
            "statussummary" => 'placed',
        );

        if (!empty($service)) {
            $condition['service'] = $service;
        }

        $result = $this->where($condition)->order("addtime desc")->limit(20)->select();

        // 如果当前手机号查询出来的订单至少有一条openid为0
        if(!empty($result) AND $userID > 0){
            $condition['wxuserid'] = 0;
            $val = $this->where($condition)->find();

            if ($val and $val['id'] > 0) {
                $data = array(
                    "wxuserid" => $userID
                );
                $this->data($data)->where($condition)->save();
            }
        }

        return $result;
    }

    /**
     *判断退款表是否已有要取消的订单
     */
    public function checkRefundExist($order_id)
    {
        $sql = "SELECT * FROM wx_refunds WHERE orderid = '$order_id' LIMIT 1";
        $result = $this->query($sql);
        return $result;
    }

    /**
     * finalprice <= 0 保存订单
     */
    public function updateOrders($where, $data)
    {
        $this->data($data)->where('id=' . $where)->save();
    }

    public function getOrderByCondition($condition)
    {
        return $this->where($condition)->find();
    }

    /**
     * 更新订单
     */
    public function updateOrder($where, $data)
    {
        $this->data($data)->where($where)->save();
    }

    /**
     * 获取包含支付信息的订单信息
     * @param $oid
     */
    public function getOrderWithPayinfo($orderid)
    {
        $sql = "select O.*,P.id as payid,P.pay_pattern,P.fee,P.out_trade_no,
                  P.buyer_account,P.buyer_id,
                  P.gateway_total_fee,P.gateway_notify_time
                from wx_orders O left join wx_pays P on O.id=P.order_id
                where P.pay_type='gateway' and O.id='{$orderid}'";
        $results = $this->query($sql);
        if ($results and isset($results[0]["id"])) {
            if (count($results) > 1) {
                // TODO log?
            }

            return $results[0];
        }

        return false;
    }

    public function getOrders($condition)
    {
        return $this->where($condition)->select();
    }

    public function getOrdersByOrder($condition,$order)
    {
        return $this->where($condition)->order($order)->select();
    }

    public function getOerdersByPages($condition = '1>0',$order = 'id',$startindex=1,$size=20){

        $sql =  "SELECT * FROM lockorder INNER JOIN (SELECT * FROM lockorder WHERE {$condition} ORDER BY {$order} LIMIT {$startindex} , {$size}) AS product2 USING ( id )";

        $results = $this->query($sql);

        return $results;
    }

    public function getCount($condition = '1>0'){

        $results = $this->where($condition)->count();;

        return $results;
    }

}


// end